﻿
@{
    ViewData["Title"] = "AddClient";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1 style="text-align:center">新增客户信息</h1>

<body >
    <form class="layui-form" onsubmit="return false" style="margin:5px auto;padding-left:20px">
        <div class="layui-form-item">
            <label class="layui-form-label">客户类型</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="radio" id="clientType" name="clientType" value="true" title="个人">
                <input type="radio" id="clientType" name="clientType" value="false" title="企业">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">负责人手机号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientTel" name="clientTel" autocomplete="off" lay-verify="required" placeholder="负责人手机号" class="layui-input">
            </div>

            <label class="layui-form-label">负责人姓名</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientName" name="clientName" autocomplete="off" lay-verify="required" placeholder="负责人姓名" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">客户名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="client" name="client" autocomplete="off" lay-verify="required" placeholder="客户名称" class="layui-input">
            </div>

            <label class="layui-form-label">身份证号码</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientCard" name="clientCard" autocomplete="off" lay-verify="required" placeholder="身份证号码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所在地</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientLocation" name="clientLocation" autocomplete="off" lay-verify="required" placeholder="所在地" class="layui-input">
            </div>

            <label class="layui-form-label">社会信用代码</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientSocial" name="clientSocial" autocomplete="off" lay-verify="required" placeholder="社会信用代码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">录入单位</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="inputId" name="inputId">
                    <option value="">请选择</option>
                </select>
            </div>

            <label class="layui-form-label">使用单位</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="buildingId" name="buildingId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">使用状态</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="radio" id="clientState" name="clientState" value=1 title="启用">
                <input type="radio" id="clientState" name="clientState" value=2 title="停用">
            </div>

            <label class="layui-form-label">服务人员</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientService" name="clientService" autocomplete="off" lay-verify="required" placeholder="服务人员" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>

<script>
    layui.use(['form', 'laydate', 'util', 'layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        var $ = layui.$;

        // 自定义验证规则
        form.verify({
            pass: function (value) {
                if (!/(.+){6,12}$/.test(value)) {
                    return '密码必须 6 到 12 位';
                }
            }
        });

        // 指定开关事件
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关 checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是 ON|OFF', data.othis)
        });

        // 提交事件
        form.on('submit(demo1)', function (data) {
            //var field = data.field; // 获取表单字段值

            clientType = document.querySelector('input[name="clientType"]:checked').value == "true";  //客户类型
            clientTel = $('#clientTel').val();  //负责人手机号
            clientName = $('#clientName').val(); //负责人姓名
            client = $('#client').val();   //客户名称
            clientCard = $('#clientCard').val(); //身份证号码
            clientLocation = $('#clientLocation').val();  //所在地
            clientSocial = $('#clientSocial').val();  //社会信用代码
            inputId = $('#inputId').val();       //录入单位
            buildingId = $('#buildingId').val();       //使用单位
            clientState = document.querySelector('input[name="clientState"]:checked').value;   //使用状态
            clientService = $('#clientService').val();  //服务人员

            var formData = {
                clientType: clientType,
                clientTel: clientTel,
                clientName: clientName,
                client: client,
                clientCard: clientCard,
                clientLocation: clientLocation,
                clientSocial: clientSocial,
                inputId: inputId,
                buildingId: buildingId,
                clientState: clientState,
                clientService: clientService,
            };
            //console.log(formData)
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Client/AddClientInfo', // 替换为您的后端API接口地址
                type: 'post',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function (res) {
                    if (res > 0) {
                        layer.alert('添加成功')
                        location.href = '/Supplier/ClientShow'
                        //示范对话框所有内置图标
                        // layer.alert('添加成功', {
                        //     icon: 1,
                        //     shadeClose: true,
                        //     title: "供应商添加",
                        // }, function () {
                        //     location.href = '/Supplier/AddSupplier'
                        // });
                    } else {
                        //layer.msg(res.msg, { icon: 2 });
                        alert("添加失败！");
                    }
                },
                error: function () {
                    layer.msg('网络错误，请稍后重试', { icon: 2 });
                }
            });


            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#date'
        });

        // 普通事件
        util.on('lay-on', {
            // 获取验证码
            "get-vercode": function (othis) {
                var isvalid = form.validate('.demo-phone'); // 主动触发验证，v2.7.0 新增
                // 验证通过
                if (isvalid) {
                    layer.msg('手机号规则验证通过');
                    // 此处可继续书写「发送验证码」等后续逻辑
                    // …
                }
            }
        });

        //绑定录入单位下拉框
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Cottage/UnitsList',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    var departDate = res;
                    var select = $('#inputId');
                    $.each(departDate, function (index, item) {
                        var option = $('<option>').val(item.id).text(item.name);
                        select.append(option);
                    })
                    //手动触发 layui组件的渲染
                    layui.form.render('select');
                },
            })
        })

        //绑定使用单位下拉框
        $(document).ready(function () {
            $.ajax({
                url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Cottage/UnitsList',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    var departDate = res;
                    var select = $('#buildingId');
                    $.each(departDate, function (index, item) {
                        var option = $('<option>').val(item.id).text(item.name);
                        select.append(option);
                    })
                    //手动触发 layui组件的渲染
                    layui.form.render('select');
                },
            })
        })
    });
</script>